﻿using Furion.DatabaseAccessor;
using GsOrganization.Common.Extend;
using GsOrganization.Common.Helper;
using GsOrganization.TcpPacket;
using GsOrganization.TcpServer;
using GSOrganization.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace GSOrganization.Application.TcpCallBack
{
    /// <summary>
    /// TCP回调服务类，用于处理TCP协议相关的回调逻辑，不继承ApiController，因为不需要暴露API接口给前端调用
    /// </summary>
    public class TcpCallBackService : ITransient
    {
        public void SetTcpCallBack()
        {
            //示例添加回调
           TcpResourcesManager.SetAction(MessageCommandId.SendHelloWorldReq, SendHelloWorldRsp);
        }

        //示例
        //private int SendCarAndLatLongDataRsp(ClientSession session, Header header, byte[] data)
        //{

        //}

        private int SendHelloWorldRsp(ClientSession session, Header header, byte[] data)
        {
            var result = -10;
            byte[] bsPayLoad = new byte[data.Length - IHeader.HeaderLength];
            Array.Copy(data, IHeader.HeaderLength, bsPayLoad, 0, bsPayLoad.Length);
            bsPayLoad = bsPayLoad.AES_Dencrypt();
            if (bsPayLoad == null)
                LogHelper.Warning($"Hello World解析失败，ClientId为{session.ClientId}");
            var console = $"收到客户端{session.ClientId}的Hello World请求，数据为：{Encoding.UTF8.GetString(bsPayLoad)}";
            Console.WriteLine(console);
            result = 0;
            return result;
        }
    }
}
